﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Presenter.Support;
using System.Web;
using System.Text;
using MS.InformationManagement.DAL.DTOs;
using System.Linq;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class TabsTomosExpediente : BaseControl
    {
        public List<Tomo> LstTomos
        {
            get {
                if(Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "ListTomos"] == null)
                    Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "ListTomos"] = new List<Tomo>();
                return Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "ListTomos"] as List<Tomo>;
            }
            set 
            { 
                Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "ListTomos"] = value;
                if (value != null)
                {
                    ctnLiteral.Controls.Clear();
                    ctnDivs.Controls.Clear();

                    StringBuilder javascriptBuilder = new StringBuilder("<script type='text/javascript'>");
                    string div1 = "<div runat='server' id='tab{0}'><div id='Tomo{0}' class='tabdiv'>";
                    string div2 = "</div></div>";
                    string ctrl = "<li runat='server' id='liTomo{0}'><a href='#Tomo{0}'>Tomo {0}</a></li>";

                    foreach (var item in value)
                    {
                        ctnLiteral.Controls.Add(new LiteralControl(string.Format(ctrl, item.Title)));

                        ctnDivs.Controls.Add(new LiteralControl(string.Format(div1, item.Title)));
                        ExpedientePorTomo nuevoControl = (ExpedientePorTomo)Page.LoadControl(@"~/_controltemplates/InformationManagerControls/ExpedientePorTomo.ascx");
                        nuevoControl.EnableAddRemove = EnableAddRemove;
                        nuevoControl.IdExpediente = item.NúmeroExpediente;
                        nuevoControl.IdTomo = item.Id.ToString();
                        ctnDivs.Controls.Add(nuevoControl);
                        ctnDivs.Controls.Add(new LiteralControl(div2));

                        javascriptBuilder
                            .Append(string.Format("function exportVolumeItems{0}", item.Id.ToString()))
                            .Append("(){")
                            .Append(string.Format("var bID='{0}';", item.NúmeroExpediente))
                            .Append(string.Format("var tID='{0}';", item.Id.ToString()))
                            .Append(string.Format("var url = '{0}/_layouts/Ms.InformationManagement.WP/ExportHandler.ashx?action=folderbyvolume&expID='+bID+'&volID='+tID;", SPContext.Current.Web.Url))
                            .Append("window.open(url, '_blank');}");
                    }

                    javascriptBuilder.Append("</script>");
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "functions", javascriptBuilder.ToString(), false);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsPostBack && Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "ListTomos"] != null)
                    LstTomos = Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "ListTomos"] as List<Tomo>;
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        public bool EnableAddRemove
        {
            get {
                if (ViewState["AddRemoveEnable"] != null)
                    Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "AddRemoveIncludeEnable"] = Convert.ToBoolean(ViewState["AddRemoveEnable"]);
                return Convert.ToBoolean(Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "AddRemoveIncludeEnable"]);
            }
            set 
            {
                Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "AddRemoveIncludeEnable"] = value;
            }
        }
    }
}
